package com.tm.controller;

import com.tm.model.Dept;
import com.tm.service.deptService;
import com.tm.util.RestReturnModel;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author huangp
 * @create 2021-09-12 11:24
 */
@CrossOrigin
@RestController
@RequestMapping("dept")
public class deptController {
    @Autowired
    private  deptService deptService;


    @ApiOperation(value = "获取所有数据",notes = "获取 部门数据" )
    @ApiImplicitParam(name = "id",value = "部门id",required = true,dataType = "number")
    @GetMapping("allDept")
    public Map<String,Object> allDept(){
        Map<String, Object> map = new HashMap<>();
        try {

            List<Map<String,Object>> list = deptService.allDept();
            map.put("flag",200);
            map.put("data",list);
        }catch (Exception e){
            e.printStackTrace();
            map.put("flag",202);
        }
        return  map;
    }
    //新增
    @ApiOperation(value = "新增部门",notes = "新增部门" )
    @PostMapping
    public RestReturnModel addDept(@RequestBody Dept dept){
                   deptService.addDept(dept);
        return RestReturnModel.SUCCESS(null);
    }
    @ApiOperation(value = "修改部门",notes = "修改部门" )
    @PutMapping
    public Map<String,Object> updateData(@RequestBody Dept dept){
        Map<String, Object> map = new HashMap<>();
        try {
                deptService.updateData(dept);
                map.put("flag", 200);
        }catch (Exception e){
            e.printStackTrace();
            map.put("flag",202);
        }
        return  map;
    }

    //回显
    @ApiOperation(value = "回显选中数据",notes = "回显选中数据" )
    @ApiImplicitParam(name = "id",value = "部门id",required = true,dataType = "number")
    @GetMapping("{id}")
    public Map<String,Object> queryDeptById(@PathVariable Integer id){
        Map<String, Object> map = new HashMap<>();
        try {
            Dept dept = deptService.queryDeptById(id);
            map.put("flag",200);
            map.put("data",dept);
        }catch (Exception e){
            e.printStackTrace();
            map.put("flag",202);
        }
        return  map;
    }

    //删除
    @ApiOperation(value = "删除部门",notes = "删除部门" )
    @ApiImplicitParam(name = "id",value = "b部门id",required = true,dataType = "number")
    @DeleteMapping("{id}")
    public Map<String,Object> delDeptById(@PathVariable Integer id){
        Map<String, Object> map = new HashMap<>();
        try {
            deptService.delDeptById(id);
            map.put("flag",200);
        }catch (Exception e){
            e.printStackTrace();
            map.put("flag",202);
        }
        return  map;
    }
    @ApiOperation(value = "导出")
    @GetMapping("exportFile")
    public void exportFile(HttpServletResponse response ){
        deptService.exportFile(response);
    }
    @ApiOperation(value = "导入")
    @PostMapping ("uploadFile")
    public void uploadFile(MultipartFile dataFile) {
        deptService.uploadFile(dataFile);
    }

}
